
import constant from '@/utils/constant'
import storage from '@/utils/storage'

import { getMyInfo } from '@/api/app/master/master.js'

const master = {
    state: {
        masterId: storage.get(constant.masterId),
        masterName: storage.get(constant.masterName),
        masterPhone: storage.get(constant.masterPhone),
        masterAvatar: storage.get(constant.masterAvatar),       

    },
    mutations: {
        SET_MASTERID: (state, masterId) => {
            state.masterId = masterId
            storage.set(constant.masterId, masterId) 
        },
        SET_MASTERNAME: (state, masterName) => {
            state.masterName = masterName
            storage.set(constant.masterName, masterName)
        },
        SET_MASTERPHONE: (state, masterPhone) => {
            state.masterPhone = masterPhone
            storage.set(constant.masterPhone, masterPhone)
        },
         SET_MASTERAVATAR: (state, masterAvatar) => {
            state.masterAvatar = masterAvatar
            storage.set(constant.masterAvatar, masterAvatar)
        },
    },
    actions: { 
        // 获取师傅信息
        GetMasterInfo({ commit }) {
            return new Promise((resolve, reject) => {   
                getMyInfo().then(response => {
                    const data = response.data
                    commit('SET_MASTERID', data.id)
                    commit('SET_MASTERNAME', data.masterName)
                    commit('SET_MASTERPHONE', data.masterPhone)
                    commit('SET_MASTERAVATAR', data.avatar)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}

export default master   